package com.cheney.common.sensitive.core;

import com.cheney.common.sensitive.handler.LogbackMessageHandler;
import com.cheney.common.sensitive.util.SensitiveUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.core.convert.converter.Converter;

/**
 * 脱敏转换器
 */
public class SensitiveConverter implements Converter<String, LogbackMessageHandler> {

    private final ApplicationContext applicationContext;

    public SensitiveConverter(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public LogbackMessageHandler convert(String source) {
        try {
            return SensitiveUtil.checkAndGetInstance(source, LogbackMessageHandler.class, applicationContext);
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            throw new IllegalArgumentException("Convert convert [" + source + "] to LogbackMessageHandler.", e);
        }
    }
}
